/*
	Copyright (C) 2011 Salil Bhagurkar

	This file is part of illusion

	illusion is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of
	the License, or (at your option) any later version.

	illusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with illusion.
	If not, see <http://www.gnu.org/licenses/>.
*/

#include <kernel/list.h>
#include <kernel/glist.h>
#include <kernel/kmalloc.h>

struct generic_list *generic_list_add(struct generic_list **head, struct generic_list **tail, void *data)
{
	struct generic_list *node = (struct generic_list *)kmalloc(sizeof(struct generic_list), "glist");
	node->data = data;
	node->next = node->prev = null;
	list_attach((*head), (*tail), node);
	return node;
}

struct generic_list *generic_list_find(struct generic_list *head, void *data)
{
	struct generic_list *temp;
	list_for(head, temp) {
		if(temp->data == data)
			return temp;
	}
	return null;
}

bool generic_list_remove(struct generic_list **head, struct generic_list **tail, void *data)
{
	struct generic_list *node = generic_list_find((*head), data);
	if(node != null) {
		generic_list_remove_node(head, tail, node);
		return true;
	}
	return false;
}

void generic_list_remove_node(struct generic_list **head, struct generic_list **tail, struct generic_list *node)
{
	list_detach((*head), (*tail), node);
	kfree(node, sizeof(struct generic_list), "glist");
}
